// PreferencesPlugin.kt
package com.sanji.audio_test

import android.app.Activity
import android.net.Uri
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler

/**
 * 偏好设置插件
 * 提供Flutter访问和修改应用偏好设置的能力
 */
class PreferencesPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
    private var channel: MethodChannel? = null
    private var activity: Activity? = null
    private var preferences: Preferences? = null

    /**
     * 当插件附加到Flutter引擎时调用
     * @param flutterPluginBinding Flutter插件绑定对象
     */
    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(
            flutterPluginBinding.binaryMessenger,
            "preferences"
        )
        channel?.setMethodCallHandler(this)
    }

    /**
     * 处理来自Flutter的方法调用
     * @param call 方法调用对象，包含方法名和参数
     * @param result 方法调用结果回调
     */
    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        // 确保activity和preferences可用
        if (activity == null) {
            result.error("NO_ACTIVITY", "Activity is not available", null)
            return
        }

        if (preferences == null) {
            preferences = Preferences(activity!!)
        }

        when (call.method) {
            "isEnabled" -> {
                // 获取启用状态
                result.success(preferences?.isEnabled ?: false)
            }
            "setEnabled" -> {
                // 设置启用状态
                val enabled = call.arguments as? Boolean ?: false
                preferences?.isEnabled = enabled
                result.success(null)
            }
            "getOpeningStatement" -> {
                // 获取开场白文件URI
                val uri = preferences?.openingStatement?.toString()
                result.success(uri)
            }
            "setOpeningStatement" -> {
                // 设置开场白文件URI
                val uriString = call.arguments as? String
                val uri = if (uriString != null) Uri.parse(uriString) else null
                preferences?.openingStatement = uri
                result.success(null)
            }
            else -> result.notImplemented()
        }
    }

    /**
     * 当插件从Flutter引擎分离时调用
     * @param binding Flutter插件绑定对象
     */
    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        channel?.setMethodCallHandler(null)
        channel = null
    }

    /**
     * 当插件附加到Activity时调用
     * @param binding Activity插件绑定对象
     */
    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        activity = binding.activity
        if (activity != null) {
            preferences = Preferences(activity!!)
        }
    }

    /**
     * 当插件从Activity分离时调用
     */
    override fun onDetachedFromActivity() {
        activity = null
        preferences = null
    }

    /**
     * 当Activity因配置更改重新附加时调用
     * @param binding Activity插件绑定对象
     */
    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
        onAttachedToActivity(binding)
    }

    /**
     * 当Activity因配置更改分离时调用
     */
    override fun onDetachedFromActivityForConfigChanges() {
        onDetachedFromActivity()
    }
}